package com.iking.housekeeping.service.user;

import java.util.List;
import java.util.concurrent.TimeUnit;

import com.iking.housekeeping.entity.dept.SysDept;
import com.iking.housekeeping.service.dept.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.iking.housekeeping.dao.user.SysUserMapper;
import com.iking.housekeeping.entity.menu.SysMenu;
import com.iking.housekeeping.entity.user.SysUser;
import com.iking.housekeeping.redis.RedisObjectService;
import com.iking.housekeeping.service.role.RoleService;
import com.iking.housekeeping.util.token.WebTokenUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * 系统登录
 * @author 张重阳
 *
 */
@Service
public class LoginService {
	
	@Autowired
	private SysUserMapper sysUserMapper;
	
	@Autowired
	private RedisObjectService<String> redisObjectString;
	
	@Autowired
	private RoleService roleSerivce;
	@Autowired
	private DeptService deptService;
	/**
	 * 根据用户名查找用户
	 * @param loginName
	 * @return
	 */
	public SysUser selectUserByLoginName(String loginName) {
		return sysUserMapper.selectByLoginName(loginName);
	}

	/**
	 * 判断用户密码是否正确
	 * @param user
	 * @param password
	 * @return
	 */
	public Boolean checkPassword(SysUser user, String password) {
		if(user == null) {
			return false;
		}
		if(user.getfPassword().equals(password)){
			return true;
		}
		return false;
	}

	/**
	 * 判断用户登录是否过期
	 * @param webToken
	 * @return
	 */
	public Boolean loginCheck(String webToken) {
		//获取用户Id
		String userId = WebTokenUtil.getUserIdByWebToken(webToken);
		//获取redis中存储的webToken
		String redisWebToken = redisObjectString.get("webToken:" + userId);
		//判断webToken是否相等
		return webToken.equals(redisWebToken);
	}

	/**
	 * 将用户webToken存入redis中
	 * @param userId
	 * @param webToken
	 */
	public void saveWebToken(String userId, String webToken) {
		redisObjectString.set("webToken:" + userId, webToken, 1L, TimeUnit.DAYS);
	}

	/**
	 * 删除redis中的用户webToken
	 * @param userId
	 */
	public void deleteWebToken(String userId) {
		redisObjectString.remove("webToken:" + userId);
	}

	/**
	 * 通过用户名获取对应菜单
	 *  @param fLoginName
	 *  @return List<SysMenu>
	 * @author: gf
	 */
	public List<SysMenu> getMenuByLoginName(String fLoginName) {
		//通过用户名获取用户ID
		String fUserId = sysUserMapper.getfIdByLoginName(fLoginName);
		List<SysMenu> menuList = roleSerivce.selectRoleByUserId(fUserId);
		return menuList;
	}
	/**
	 * 获取用户权限
	 *  @param fStreetCommunity
	 *  @param menuList
	 *  @return int
	 */
	public  int select( String fStreetCommunity, List<SysDept> menuList){
		    int result=0;
		   List<SysDept> list = deptService.selectdepCod(fStreetCommunity);
			if(fStreetCommunity==null || "".equals(fStreetCommunity)){
				// 判断当前登录人是否为管理员
				if ("".equals(menuList.get(0).getfDeptPCode()) || menuList.get(0).getfDeptPCode() == null) {
					// 查看所有区域数据
					result=1;
				} else {
					if (!list.get(0).getfDeptCode().equals(menuList.get(0).getfDeptPCode())) {
						// 查看二级服务数据
						result=2;
					} else {
						// 查看区域务数据
						result=3;
					}
				}
			}else {
				//管理员
				if(  menuList.get(0).getfDeptPCode() == null){
					result=4;
				}
               //区域管理员
				else  if(list.get(0).getfDeptPCode().equals("01")){
					result=4;
						if(!list.get(0).getfDeptCode().equals(menuList.get(0).getfDeptCode())){
							result=0;
						}
					}
					else if(!list.get(0).getfDeptPCode().equals("01")){
					result=4;
						if(!list.get(0).getfDeptPCode().equals(menuList.get(0).getfDeptPCode())){
							result=0;
						}
					}
				else{
					result=4;
					if(!fStreetCommunity.equals(menuList.get(0).getfId())){
						result=0;
					}
				}
			}

		return result;
	}
}
